package com.zkingsoft.services.distribution.impl;

import com.zkingsoft.services.distribution.DistributionCategoryService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.distribution.DistributionCategory;
import com.zkingsoft.dao.distribution.DistributionCategoryDao;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-06-23 09:14
 */
@Service("distributionCategoryService")
public class  DistributionCategoryServiceImpl implements    DistributionCategoryService  {

	
	@Autowired
	private DistributionCategoryDao distributionCategoryDao;
	@Autowired
	private ServiceUtil serviceUtil;
	
	@Override
	public int add(DistributionCategory distributionCategory){
		if (serviceUtil.addCheckRepeat("dis_category", "cat_name", 
				distributionCategory.getCatName())) {
			throw new GlobleException("类别名称已经存在");
		}
		return distributionCategoryDao.insert(distributionCategory);
		
	}
	
	@Override
	public int batchAdd(List<DistributionCategory>  distributionCategoryList) {
		int num = 0;

		int c = 10000;
		int size = distributionCategoryList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= distributionCategoryList.size() ? distributionCategoryList.size() : end;
			List<DistributionCategory> insertList = distributionCategoryList.subList(begin, end);
			num += distributionCategoryDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(DistributionCategory oldDistributionCategory
	,DistributionCategory newDistributionCategory){
	
		if (!ModelUtils.isModified(oldDistributionCategory, newDistributionCategory)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDistributionCategory, newDistributionCategory);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("catId", oldDistributionCategory.getCatId());
			distributionCategoryDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(DistributionCategory distributionCategory){
	
		return distributionCategoryDao.updateByModel(distributionCategory);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
	
		return distributionCategoryDao.deleteByIds(list);
	
	}
	
	/**
	 * 获取功能下的子功能id，包含功能自己的id
	 * 
	 * @param id
	 * @param ids
	 * @return
	 */
	private List<Long> getFunctionChirds(Long id, List<Long> ids) {
		DistributionCategory distributionCategory = new DistributionCategory();
		distributionCategory.setCatParentId(id);
		// 获取子节点
		List<DistributionCategory> list = distributionCategoryDao.selectByModel(distributionCategory);
		if (!list.isEmpty()) {
			for (DistributionCategory dis : list) {
				// 删除子节点
				getFunctionChirds(dis.getCatId(), ids);
			}
		}
		ids.add(id);
		return ids;

	}

	@Override
	public int removeById(Long catId){
		if (catId == null) {
			return 0;
		}
		List<Long> ids = new ArrayList<>();
		ids = getFunctionChirds(catId, new ArrayList<Long>());
		
		return distributionCategoryDao.deleteByIds(ids);
	}
	
	@Override
	public int removeByModel(DistributionCategory distributionCategory){
	
		return distributionCategoryDao.deleteByModel(distributionCategory);
	
	}
	
	
	@Override
	public List<DistributionCategory> findInPage(DistributionCategory distributionCategory,  PaginationVO pageVo){
	
		return distributionCategoryDao.selectInPage(distributionCategory , pageVo);
	
	}
	
	@Override
	public List<DistributionCategory> findByModel(DistributionCategory distributionCategory){
	
		return distributionCategoryDao.selectByModel(distributionCategory);
	
	}
	
	@Override
	public int  findTotal(DistributionCategory distributionCategory){
	
		return distributionCategoryDao.selectTotalRecord(distributionCategory);
	
	}
	
	@Override
	public DistributionCategory  findById(Long catId){
	
		return distributionCategoryDao.selectById(catId);
	
	}

   	
	
	
}